package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetPictureBookMainPageBean;
import com.rjwh.dingdong.client.bean.localbean.DrawBookMainPageTopic;
import com.rjwh.dingdong.client.bean.localbean.GetPictureBookMainPageBean;
import com.rjwh.dingdong.client.bean.localbean.PictureBookMainPageBannerData;
import com.rjwh.dingdong.client.bean.localbean.PictureBookMainPagePictureData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.DrawBookModel;
import com.rjwh.dingdong.client.mvp.presenter.IDrawbookPresenter;
import com.rjwh.dingdong.client.mvp.view.DrawBookView;

import java.util.List;

/**
 * @author Kay
 * @ClassName DrawbookPresenter
 * @Description
 * @date 2016-6-2
 * @history 2016-6-2 author: description:
 */
public class DrawbookPresenter extends BasePresenter<DrawBookView> implements IDrawbookPresenter {

    private DrawBookModel model;

    public DrawbookPresenter(Context context, DrawBookView mMvpView) {
        super(context, mMvpView);
        model = new DrawBookModel(this);
    }

    /**
     * 获取绘本首页
     */
    @Override
    public void doPictureBookMainPage() {
        isViewAttached();
        getMvpView().hideErrorView();
        getMvpView().showLoadingView();
        model.doPictureBookMainPage();
    }

    /**
     * 请求结果
     *
     * @param businessType
     * @param mAsResult
     * @param isRefresh
     */
    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            getMvpView().hideLoadingView();
            switch (businessType) {
                case NetConstant.GETPICTUREBOOKMAINPAGE:
                    ResGetPictureBookMainPageBean resGetPictureBookMainPageBean = (ResGetPictureBookMainPageBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetPictureBookMainPageBean != null && resGetPictureBookMainPageBean.getData() != null) {
                        getMvpView().hideErrorView();
                        GetPictureBookMainPageBean data = resGetPictureBookMainPageBean.getData();
                        List<PictureBookMainPageBannerData> bannerlist = data.getBannerlist();
                        List<PictureBookMainPagePictureData> picturebooklist = data.getPicturebooklist();
                        List<DrawBookMainPageTopic> topiclist = data.getTopiclist();
                        if (bannerlist != null) {
                            getMvpView().loadBannerData(bannerlist);
                        }
                        if (picturebooklist != null) {
                            getMvpView().loadPicBookData(picturebooklist);
                        }
                        if (topiclist != null) {
                            getMvpView().loadTopicData(topiclist);
                        }
                    } else {
                        getMvpView().showErrorView();
                    }
                    break;
            }
        }

    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

}
